import { createRouter, createWebHistory } from 'vue-router'
import DingtalkLogin from './components/DingtalkLogin.vue'
import UserProfile from './components/UserProfile.vue'
import HomePage from './components/HomePage.vue'

const routes = [
  {
    path: '/',
    redirect: () => {
      // 如果已登录，跳转到首页；否则跳转到登录页
      const token = localStorage.getItem('access_token')
      return token ? '/home' : '/login'
    }
  },
  {
    path: '/login',
    name: 'Login',
    component: DingtalkLogin
  },
  {
    path: '/home',
    name: 'Home',
    component: HomePage
  },
  {
    path: '/dingtalk/callback',
    name: 'DingtalkCallback',
    component: () => import('./components/DingtalkCallback.vue')
  },
  {
    path: '/profile',
    name: 'Profile',
    component: UserProfile
  },
  {
    path: '/post/:id',
    name: 'PostDetail',
    component: () => import('./components/PostDetail.vue')
  },
  {
    path: '/real-name-auth',
    name: 'RealNameAuth',
    component: () => import('./views/RealNameAuth.vue')
  },
  {
    path: '/design-patterns',
    name: 'DesignPatterns',
    component: () => import('./components/DesignPatternsDemo.vue')
  },
  {
    path: '/customer-service/chat',
    name: 'CustomerServiceChat',
    component: () => import('./components/CustomerServiceChat.vue')
  },
  {
    path: '/customer-service/admin',
    name: 'ServiceAdmin',
    component: () => import('./components/ServiceAdmin.vue')
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

export default router

